var index=1;
define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'echarts'], function ($, undefined, Backend, Table, Form, Echarts) {

    var Controller = {
        index: function () {

        },
        fromexten: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'facrm/setting/cloudcall/fromexten/key/'+Config.callkey,
                    add_url: 'facrm/setting/cloudcall/fromextenedit/key/'+Config.callkey,
                    edit_url: 'facrm/setting/cloudcall/fromextenedit/key/'+Config.callkey,
                    del_url: 'facrm/setting/cloudcall/fromextendel/key/'+Config.callkey,
                    multi_url: '',
                    table: '',
                }
            });

            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {field: 'id', title: 'ID'},
                        {field: 'admin_id', title: __('员工'),addclass: 'selectpage',
                            formatter: function (v, r) {
                                if (!r.admin) return '-';
                                return r.admin.nickname;
                            },
                            extend: 'data-source="facrm/common/selectpage/model/admin?type=all" data-field="nickname" data-orderBy="id desc"'},
                        {field: 'from_exten', title: __('坐席工号'), operate: "LIKE"},
                        {field: 'exten_type', title: __('外呼方式'), operate: "="},
                        {
                            field: 'status',
                            title: __('Status'),
                            formatter: Table.api.formatter.status,
                            searchList: {0: __('禁用'), 1: __('正常')}
                        },
                        {
                            field: 'create_time',
                            title: __('创建时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true
                        },
                        {
                            field: 'update_time',
                            title: __('修改时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                        }

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        fromextenedit:function(){
            Controller.api.bindevent();
        },

        add: function () {

            Controller.api.bindevent();

        },
        edit: function () {
            Controller.api.bindevent();

        },
        log: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'facrm/setting/cloudcall/log',
                    multi_url: '',
                    table: '',
                }
            });

            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {field: 'id', title: 'ID'},
                        {field: 'admin_id', title: __('员工'),addclass: 'selectpage',
                            formatter: function (v, r) {
                                if (!r.admin) return '-';
                                return r.admin.nickname;
                            },
                            extend: 'data-source="facrm/common/selectpage/model/admin?type=all" data-field="nickname" data-orderBy="id desc"'},
                        {field: 'from_exten', title: __('坐席工号'), operate: "LIKE"},
                        {field: 'exten_type', title: __('外呼方式'), operate: "="},
                        {field: 'call_no', title: __('号码'), operate: "LIKE"},
                        {field: 'source', title: __('类型'), operate: "LIKE"},
                        {field: 'name', title: __('联系人'), operate: "LIKE"},
                        {field: 'called_no', title: __('被呼号码'), operate: "LIKE"},
                        {
                            field: 'status',
                            title: __('Status'),
                            formatter: Table.api.formatter.status,
                            searchList: {0: __('未同步'), 1: __('已同步'), '-1': __('未接通')}
                        },
                        {
                            field: 'record_file',
                            title: __('录音'),
                            operate: "LIKE",
                            align: 'left',
                            formatter: function (value, row, index) {
                                if (!value) return '-';
                                return '<a href="javascript:void(0);" data-url="' +value
                                    + '" class="btn-dialog" data-title="查看录音">查看录音</a>';
                            }
                        },
                        {field: 'call_time_length', title: __('通话时长(秒)'), operate: 'BETWEEN', sortable: true},
                        {
                            field: 'create_time',
                            title: __('创建时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true
                        },
                        {
                            field: 'update_time',
                            title: __('修改时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                        }

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        statistics: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'facrm/setting/cloudcall/statistics',
                    add_url: '',
                    edit_url: '',
                    del_url: '',
                    multi_url: '',
                    table: '',
                }
            });

            var table = $("#table");

            //在普通搜索渲染后
            table.on('post-common-search.bs.table', function (event, table) {

            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                commonSearch: true,
                search: false,
                searchFormVisible: true,
                columns: [
                    [
                        {field: 'admin_id', title: '员工',
                            addclass: 'selectpage',
                            extend: 'data-source="facrm/common/selectpage/model/admin?type=all" data-field="nickname" data-orderBy="id desc"'},
                        {field: 'admin.nickname', title: __('姓名'), operate: false},
                        {field: 'count', title: __('呼叫数量'), operate: false},
                        {field: 'call_time_length', title: __('呼叫时长(分)'), operate: false},
                        {field: 'avg', title: __('平均时长(每通)'), operate: false,formatter: function (val,row,index) {
                            var avg=row.call_time_length/row.count;
                            return avg.toFixed(2);

                            }},

                        {
                            field: 'create_time',
                            title: __('时间段'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            visible:false,
                            extend: 'autocomplete="off"'
                        },

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);



        },

        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"),function () {
                    parent.window.location.reload();
                });
                $("input[type=radio][name='row[config]']").change(function(){
                    var type=$(this).val();
                    if(type==1){
                        $('.table-responsive').show();
                    }else{
                        $('.table-responsive').hide();
                    }
                });
            },
            func: {

                getUrlParam(name) {
                    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
                    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
                    if (r != null) return unescape(r[2]); return null; //返回参数值
                },

            }
        }
    };
    return Controller;
});


